<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_deleter.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/item_types/tcms_menu_item_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');


class VPage_Del_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$v2 = $this->create_view('not_exist');
		$this->add_view($v2);

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete', Tangra_Parameter_Method::GET, 'del'), 'delete_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('delete_confirmed', Tangra_Parameter_Method::GET, 'del_confirmed'), 'delete_confirmed');
	}


	protected function delete_event($vpage_id) {
		$dbc = $this->get_dbc();

		$category_id = $this->get_transit_var('category', true);
		if ($category_id) {
			$category = new TCMS_Category_DBC();

			if (!$category->load_by_id($dbc, $category_id)) {
				throw new Tangra_Module_Exception('Cannot load TCMS_Category_DBC with id = '.$category_id);
			}

			$this->export('category', $category);
		}

		$vpage = new TCMS_VPage_DBC();
		if ($vpage->load_by_id($dbc, $vpage_id)) {
			$this->export('vpage', $vpage);

			$items_linking_to_vpage = TCMS_Menu_Item_VPage_DBC::load_items_linking_to_vpage($dbc, $vpage_id);
			$this->export('items_linking_to_vpage', $items_linking_to_vpage);

			$view = $this->get_view('default');
		} else {
			$view = $this->get_view('not_exist');
		}

		return $view;
	}


	protected function delete_confirmed($vpage_id) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			$config = $this->get__config();
			TCMS_VPage_Deleter::delete($dbc, $vpage_id, $config->get_hidden_dir().'inc/');
			$context = $this->get__context();
			if ($context->exists_in_get('mi')) {
				$mi = $context->get_from_get('mi');
				if ($mi == 1) {
					$menu_items = TCMS_Menu_Item_VPage_DBC::load_ids_for_items_linking_to_vpage($dbc, $vpage_id);
					foreach($menu_items as $mi) {
						TCMS_Menu_Item_DBC::delete_cascade($dbc, $mi, $this->get__config()->get_hidden_dir().'inc/');
					}
				} elseif ($mi == 2) {
					TCMS_Menu_Item_VPage_DBC::unpublish_items_for_vpage($dbc, $vpage_id);
				}
			}

			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$category_id = $this->get_transit_var('category', true);
		if ($category_id) {
			$redir = $this->create_redirect_view('admin/modules/tangra_cms/category/category_content.php', array('category' => $category_id));
		} else {
			$redir = $this->create_redirect_view('admin/modules/tangra_cms/vpage/vpages.php');
		}

		return $redir;
	}


	protected function default_event() {
		throw new Tangra_Module_Exception('Expecting "delete" parameter.');
	}
}